import java.sql.*; // JDBC stuff.
import java.io.*;  // Reading user input.

public class StudentPortal
{
	/* This is the driving engine of the program. It parses the
	 * command-line arguments and calls the appropriate methods in
	 * the other classes.
	 *
	 * You should edit this file in two ways:
	 * 	1) 	Insert your database username and password (no @medic1!)
	 *		in the proper places.
	 *	2)	Implement the three functions getInformation, registerStudent
	 *		and unregisterStudent.
	 */
	public static void main(String[] args)
	{
		if (args.length == 1) {
			try {
				DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
				String url = "jdbc:oracle:thin:@tycho.ita.chalmers.se:1521/kingu.ita.chalmers.se";
				String userName = "VTDA357_020"; // Your username goes here!
				String password = "VTDA357_020"; // Your password goes here!
				Connection conn = DriverManager.getConnection(url,userName,password);

				String student = args[0]; // This is the identifier for the student.
				BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
				System.out.println("Welcome!");
				while(true) {
					System.out.println("Please choose a mode of operation:");
					System.out.print("? > ");
					String mode = input.readLine();
					if ((new String("information")).startsWith(mode.toLowerCase())) {
						/* Information mode */
						getInformation(conn, student);
					} else if ((new String("register")).startsWith(mode.toLowerCase())) {
						/* Register student mode */
						System.out.print("Register for what course? > ");
						String course = input.readLine();
						registerStudent(conn, student, course);
					} else if ((new String("unregister")).startsWith(mode.toLowerCase())) {
						/* Unregister student mode */
						System.out.print("Unregister from what course? > ");
						String course = input.readLine();
						unregisterStudent(conn, student, course);
					} else if ((new String("quit")).startsWith(mode.toLowerCase())) {
						System.out.println("Goodbye!");
						break;
					} else {
						System.out.println("Unknown argument, please choose either information, register, unregister or quit!");
						continue;
					}
				}
				conn.close();
			} catch (SQLException e) {
				System.err.println(e);
				System.exit(2);
			} catch (IOException e) {
				System.err.println(e);
				System.exit(2);
			}
		} else {
			System.err.println("Wrong number of arguments");
			System.exit(3);
		}
	}

	static void getInformation(Connection conn, String student) throws SQLException
	{
            Statement stmt = conn.createStatement();
            //General Information
            ResultSet rs = stmt.executeQuery("SELECT * FROM StudentsFollowing WHERE s_socialnumber = '" + student + "'");
            if (rs.next()){
                System.out.println("Information for student " + student);
                System.out.println("-------------------------------------");
                System.out.println("Name: " + rs.getString("s_name"));
                System.out.println("Programme: " + rs.getString("btb_prog"));
                System.out.println("Brach: " + rs.getString("btb_branch") + "\n");
            }
            else
                System.out.println("Error: Student doesn't exists");
            //Read courses
            rs = stmt.executeQuery("SELECT * FROM FinishedCourses WHERE s_socialnumber = '" + student + "'");
            System.out.println("Read courses (name (code), credits: grade):");
            while (rs.next()){
                System.out.println(rs.getString("c_name") + " (" + rs.getString("c_code") + "), " + rs.getString("c_credits") + "p: " + rs.getString("a_grade"));
            }
            //Registered courses
            rs = stmt.executeQuery("SELECT * FROM Registrations WHERE s_socialnumber = '" + student + "' AND STATUS = 'registered'");
            System.out.println("\nRegistered courses (name (code), credits: status):");
            while (rs.next()){
                System.out.println(rs.getString("c_name") + " (" + rs.getString("c_code") + "), " + rs.getString("c_credits") + "p: registered");
            }
            //Registered courses in waiting list
            rs = stmt.executeQuery("SELECT * FROM Registrations WHERE s_socialnumber = '" + student + "' AND STATUS = 'waiting'");
            while (rs.next()){
                System.out.println(rs.getString("c_name") + " (" + rs.getString("c_code") + "), " + rs.getString("c_credits") + "p: waiting as nr " + rs.getString("POSITION"));
            }
            //Global statistics
            rs = stmt.executeQuery("SELECT * FROM PathToGraduation WHERE STUDENT = '" + student + "'");
            rs.next();
            System.out.println("\nSeminar courses taken: " + rs.getString("SEMINAR_COURSES"));
            System.out.println("Math credits taken:  " + rs.getString("MATH_CREDITS"));
            System.out.println("Research credits taken: " + rs.getString("RESEARCH_CREDITS"));
            System.out.println("Total credits taken: " + rs.getString("TOTAL_CREDITS"));
            System.out.println("Fulfills the requirements for graduation: " + rs.getString("GRADUATED") + "\n");          
	}


	static void registerStudent(Connection conn, String student, String course)
	{
            System.out.println("registerStudent");
		// Your implementation here
	}

	static void unregisterStudent(Connection conn, String student, String course)
	{
            System.out.println("unregisterStudent");
		// Your implementation here
	}
}
